package com.example.zy5_project_jetpack.Manager;

import android.util.Log;
import android.view.PixelCopy;

import com.example.zy5_project_jetpack.mvp.model.ApiService;
import com.example.zy5_project_jetpack.mvp.view.Bean.HomeBean;
import com.example.zy5_project_jetpack.mvp.view.FindFragment.Bean.RecommentBean;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Url;

public class RetrofitManager {


    public RetrofitManager() {
    }

    private static volatile RetrofitManager instance;
    private StringBuffer sf;
    private StringBuffer homesf;

    public static RetrofitManager getInstance() {
        if (instance == null) {
            synchronized (RetrofitManager.class) {
                if (instance == null) {
                    instance = new RetrofitManager();
                }
            }
        }
        return instance;
    }


    private Retrofit getRetrofit() {
        return new Retrofit.Builder()
                .baseUrl(ApiService.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                //                .client(getOkHttp())
                .build();
    }

    public <T> T getApiService(Class<T> mClass) {
        return getRetrofit().create(mClass);
    }

    public OkHttpClient getOkHttp() {
        return new OkHttpClient.Builder()
                .addInterceptor(new MyInterceptor()).build();
    }

    private class MyInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {

            Request request = chain.request();
            String method = request.method();
            HttpUrl url = request.url();
            Log.e("tag", "拦截的网址" + url);
            Response response = chain.proceed(request);

            return response;
        }
    }

    public String getHttp() throws Exception {
        URL url = new URL("http://123.56.232.18:8080/serverdemo/tag/queryTagList");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        if (urlConnection.getResponseCode() == 200) {
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String str;
            sf = new StringBuffer();
            while ((str = in.readLine()) != null) {
                sf.append(str);
            }
            Log.e("tag", sf.toString());
        }
        return sf.toString();
    }

   /* public String getHomeHttp() throws Exception{

            URL url = new URL("http://123.56.232.18:8080/serverdemo/feeds/queryHotFeedsList");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            if (con.getResponseCode() == 200) {
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String str;
                homesf = new StringBuffer();
                while ((str = in.readLine()) != null) {
                    homesf.append(str);
                }
            }
        return homesf.toString();

    }*/

    public String getImageHttp() throws Exception{

        URL url = new URL(" http://123.56.232.18:8080/serverdemo/feeds/queryHotFeedsList?pageCount=12&feedType=pics&feedId=0&userId=0");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        if (con.getResponseCode() == 200) {
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String str;
            homesf = new StringBuffer();
            while ((str = in.readLine()) != null) {
                homesf.append(str);
            }
        }
        return homesf.toString();

    }
    public String getVideoHttp() throws Exception{

        URL url = new URL("http://123.56.232.18:8080/serverdemo/feeds/queryHotFeedsList?pageCount=12&feedType=video&feedId=0&userId=0");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        if (con.getResponseCode() == 200) {
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String str;
            homesf = new StringBuffer();
            while ((str = in.readLine()) != null) {
                homesf.append(str);
            }
        }
        return homesf.toString();

    }

    public String getTextHttp() throws Exception{

        URL url = new URL("http://123.56.232.18:8080/serverdemo/feeds/queryHotFeedsList?pageCount=10&feedType=text&feedId=1578920275&userId=0");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        if (con.getResponseCode() == 200) {
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String str;
            homesf = new StringBuffer();
            while ((str = in.readLine()) != null) {
                homesf.append(str);
            }
        }
        return homesf.toString();

    }
}